package cn.highset.modules.base.controller.api;

import cn.highset.base.ApiBaseController;
import cn.highset.common.annotation.IgnoreAuth;
import cn.highset.common.utils.PageUtil;
import cn.highset.common.utils.ResultUtil;
import cn.highset.common.utils.SecurityUtil;
import cn.highset.common.vo.PageVo;
import cn.highset.common.vo.Result;
import cn.highset.modules.base.entity.Exhibitor;
import cn.highset.modules.base.entity.ExhibitorCollect;
import cn.highset.modules.base.entity.ProductType;
import cn.highset.modules.base.service.ExhibitorCollectService;
import cn.highset.modules.base.service.ExhibitorService;
import cn.highset.modules.base.service.mybatis.IExhibitorCollectService;
import cn.highset.modules.base.service.mybatis.IExhibitorService;
import cn.highset.modules.base.service.mybatis.IProductTypeService;
import cn.highset.redis.client.RedisClient;
import cn.hutool.core.collection.CollUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.Set;

/**
 * 类 描 述：API 小程序展商信息
 * 创建时间：2019-06-15 10:32
 * 创 建 人：victor
 */
@Slf4j
@RestController
@Api(description = "API展商信息")
@RequestMapping("/api/exhibitor")
@Transactional
public class ApiExhibitorController extends ApiBaseController {

    @Autowired
    private ExhibitorService exhibitorService;

    @Autowired
    private IProductTypeService iProductTypeService;
    @PersistenceContext
    private EntityManager entityManager;
    @Autowired
    private ExhibitorCollectService exhibitorCollectService;
    @Autowired
    private SecurityUtil securityUtil;
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private IExhibitorCollectService iExhibitorCollectService;
    @Autowired
    private IExhibitorService iExhibitorService;

    @ApiOperation(value = "获取展商列表")
    @IgnoreAuth
    @PostMapping("list")
    public Result<Page<Exhibitor>> list(@ModelAttribute Exhibitor exhibitor,
                                        @ModelAttribute PageVo pageVo) {

        Page<Exhibitor> page = exhibitorService.findByConditionByApi(exhibitor, PageUtil.initPage(pageVo));
        page.getContent().forEach(e -> {
            List<ProductType> pts = iProductTypeService.findByExhibitorId(e.getId(),2);
            if (CollUtil.isNotEmpty(pts)) {
                String[] ids = new String[pts.size()];
                String[] names = new String[pts.size()];
                String[] nameEns = new String[pts.size()];
                for (int i = 0, lenI = pts.size(); i < lenI; i++) {
                    ProductType pt = pts.get(i);
                    ids[i] = pt.getId();
                    names[i] = pt.getNameCn();
                }
                e.setProductTypeIds(ids);
                e.setProductTypeNames(names);
                e.setProductTypeNameEns(nameEns);

            }
            entityManager.clear();
        });
        return new ResultUtil<Page<Exhibitor>>().setData(page);
    }


    @ApiOperation(value = "获取展商详细信息")
    @IgnoreAuth
    @PostMapping("detail/{id}")
    public Result<Exhibitor> detail(@PathVariable String id) {

        Exhibitor exhibitor = exhibitorService.findById(id);
        List<ProductType> pts = iProductTypeService.findByExhibitorId(id,2);
        if (CollUtil.isNotEmpty(pts)) {
            String[] ids = new String[pts.size()];
            String[] names = new String[pts.size()];
            String[] nameEns = new String[pts.size()];
            for (int i = 0, lenI = pts.size(); i < lenI; i++) {
                ProductType pt = pts.get(i);
                ids[i] = pt.getId();
                names[i] = pt.getNameCn();
                nameEns[i] = pt.getNameEn();
            }
            exhibitor.setProductTypeIds(ids);
            exhibitor.setProductTypeNames(names);
            exhibitor.setProductTypeNameEns(nameEns);

        }
        return new ResultUtil<Exhibitor>().setData(exhibitor);
    }


    @ApiOperation(value = "收藏展商")
    @PostMapping("collect/{exhibitorId}")
    public Result<Object> collect(@PathVariable String exhibitorId) {

        String userid = securityUtil.getCurrUser().getId();
        if (exhibitorCollectService.findByExhibitorIdAndUserid(exhibitorId, userid) == null) {
            ExhibitorCollect exhibitorCollect = new ExhibitorCollect();
            exhibitorCollect.setExhibitorId(exhibitorId);
            exhibitorCollect.setUserid(userid);
            exhibitorCollectService.save(exhibitorCollect);
            iExhibitorService.incrCollectCount(exhibitorId);
            String key = "exhibitorCollect::" + userid;
            redisClient.set().sAdd(key, exhibitorId);
        }
        return new ResultUtil<Object>().setSuccessMsg("收藏成功");
    }

    @ApiOperation(value = "取消收藏展商")
    @PostMapping("collectCancel/{exhibitorId}")
    public Result<Object> collectCancel(@PathVariable String exhibitorId) {

        String userid = securityUtil.getCurrUser().getId();
        ExhibitorCollect exhibitorCollect = exhibitorCollectService.findByExhibitorIdAndUserid(exhibitorId, userid);
        if (exhibitorCollect != null) {
            exhibitorCollectService.delete(exhibitorCollect.getId());
            iExhibitorService.dimCollectCount(exhibitorId);
            String key = "exhibitorCollect::" + userid;
            redisClient.set().remove(key, exhibitorId);

        }
        return new ResultUtil<Object>().setSuccessMsg("取消收藏成功");
    }

    @ApiOperation(value = "获取收藏展商ID集合")
    @PostMapping("getCollect")
    public Result<Object> getCollect() {

        String userid = securityUtil.getCurrUser().getId();
        String key = "exhibitorCollect::" + userid;
        Set<Object> set = redisClient.set().sMembers(key);
        if (set == null || CollUtil.isEmpty(set)) {
            Set<String> exhibitorIds = iExhibitorCollectService.findExhibitorIdByUserid(userid);
            if (CollUtil.isNotEmpty(exhibitorIds)) {
                redisClient.set().sAdd(key, exhibitorIds.toArray());
            }
            return new ResultUtil<Object>().setData(exhibitorIds);
        }

        return new ResultUtil<Object>().setData(set);
    }
    @ApiOperation(value = "获取用户收藏展商列表")
    @PostMapping("collectList")
    public Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<Exhibitor>> collectList(
            @ModelAttribute PageVo pageVo) {
        String userid = securityUtil.getCurrUser().getId();
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Exhibitor> page = iExhibitorService.getCollectListByUserid(PageUtil.initMpPage(pageVo),userid);
        page.getRecords().forEach(e -> {
            List<ProductType> pts = iProductTypeService.findByExhibitorId(e.getId(),2);
            if (CollUtil.isNotEmpty(pts)) {
                String[] ids = new String[pts.size()];
                String[] names = new String[pts.size()];
                String[] nameEns = new String[pts.size()];
                for (int i = 0, lenI = pts.size(); i < lenI; i++) {
                    ProductType pt = pts.get(i);
                    ids[i] = pt.getId();
                    names[i] = pt.getNameCn();
                }
                e.setProductTypeIds(ids);
                e.setProductTypeNames(names);
                e.setProductTypeNameEns(nameEns);

            }
            entityManager.clear();
        });
        return new ResultUtil<com.baomidou.mybatisplus.extension.plugins.pagination.Page<Exhibitor>>().setData(page);
    }
}
